﻿// 漂流瓶.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include<iostream>
#include<string> 
#include<ctime>
#include<cstdlib>
#include<vector>
#include<fstream>
#include<sstream>


using namespace std;


class User {
public:
	bool Register(string oID, string oName, string oPassword);
	int Login(string iID, string iPassword,string&name);
	bool IsRepetition(string oID);
	void Loading();

private:
	vector<string> ID;
	vector<string> Name;
	vector<string> Password;

};

bool User::IsRepetition(string oID)
{
	for (int i = 0; i < ID.size(); i++)
	{
		if(ID[i]==oID)
			return true;
	}
	return false;
}

bool User::Register(string oID, string oName, string oPassword)
{
	ofstream outfile("User_in.txt", ios::app);
	if (!outfile)
	{
		cout << "请检查信息文件" << endl;
		return false;
	}

	outfile << oID << " " << oName << " " << oPassword << " " << endl;

	ID.push_back(oID);
	Name.push_back(oName);
	Password.push_back(oPassword);
	outfile.close();
	cout << "注册成功，正在跳转，请稍等" << endl;
	cout << "您的注册信息为：" << endl;
	cout << "ID:" << oID << " " << "姓名:" << oName << " " << "密码:" << oPassword << endl;
	return true;
}


int User::Login(string iID, string iPassword,string&name) {
	for (int i = 0; i < ID.size(); i++) {
	if (iID == ID[i]) {
			cout << "ID正确！" << endl;
		if (iPassword == Password[i]) {
			cout << "密码正确!" << endl;
				cout << "登陆成功！请稍后" << endl;
				cout << "您的个人信息为：" << endl;
				cout << "ID:" << iID << " " << "姓名:" << Name[i] << endl;
				name = Name[i];
				return 0;
			}
		}
	}
	cout << "您输入的账号或密码有误！请重新输入！" << endl;
	return 1;
}

void User::Loading() {
	ifstream infile("User_in.txt");
	if (!infile) {
		cout << "请检查信息文件！" << endl;
	}
	string line;
	while (getline(infile, line)) {
		string id, name, password;
		size_t pos = 0;
		//提取&加载ID信息 
		pos = line.find_first_of(' ');
		id = line.substr(0, pos);
		ID.push_back(id);
		//提取&加载姓名信息 
		name = line.substr(id.size()+1);
		pos = name.find_first_of(' ');
		name = name.substr(0, pos);
		Name.push_back(name);
		//提取&加载密码信息 
		password = line.substr(id.size()+name.size()+2);
		//cout << password << endl;
		Password.push_back(password);
		
		/*stringstream ss(line);
		getline(ss,id,' ');
		getline(ss,name,' ');
		getline(ss,password,';');
		ID.push_back(id);
		Name.push_back(name);
		Password.push_back(password);*/
	}
	infile.close();
}

bool pick_bottle(string& Rc) {
	ifstream Readfile("漂流瓶.txt", ios::in);
	if (!Readfile)//文件打开失败:返回0
	{
		cout << "无法打开文件!请检查或创建,重新进入!" << endl;
		return false;
	}
	vector<string> lines;
	string line;
	while (getline(Readfile, line)) {
		lines.push_back(line);
	}
	Readfile.close();
	if (lines.empty()) {
		cout << "暂无漂流瓶！" << endl;
		return false;
	}
	else {
		cout << "捡到一个漂流瓶：" << endl;
		size_t size = lines.size();
		srand((unsigned int)time(NULL));
		int random = rand() % (size);
		//cout << lines[random] << endl;
		size_t pos = 0;
		string str = lines[random],cname;
		const string basis = "Hide";
		pos = str.find_first_of(' ');
		cname = str.substr(0, 4);
		//cout << cname << endl;
		if (cname == basis) {
			//cout << pos;
			string str1 = str.substr(5, pos-5);
			//cout << str1<<endl;
			string str2 = str1 + " 发送者:匿名";
			cout << str2<<endl;
			str = str.substr(pos + 8);
			Rc ="Hide"+ str;
		//	cout << Rc<<endl;
		}
		else {
			str = str.substr(pos + 8);
			cout << lines[random] << endl;
			Rc = str;
		}
		
		
		//cout << str << endl;
	}
}

bool throw_bottle(string name) {
	ofstream wout("漂流瓶.txt", ios::app);
	if (!wout)
	{
		cout << "请检查信息文件" << endl;
		return false;
	}
	cout << "你想匿名发送吗?" << endl;
	char op;
	cin >> op;
	string words;
	cout << "请写下你想说的话：" << endl;
	cin >> words;
	if (op == 'y' || op == 'Y') {
		
		wout << "Hide:" << '"' << words << '"' << " " << "发送者:" << name << endl;
	}
	else {
		wout << '"' << words << '"' << " " << "发送者:" << name << endl;
	}
	cout << "您已成功扔出一个漂流瓶！" << endl;
	wout.close();
}

bool Reply_message(string name,string Rc) {
	ofstream refile("Reply_message.txt", ios::app);
	if (!refile) {
			cout << "请检查信息文件" << endl;
			return false;
		}
	cout << "请写下你想回复的消息:" << endl;
	string message;
	cin >> message;
	refile << '"' << message << '"' << " " << "回复人:" << name << " " << "回复对象:" << Rc << endl;
	refile.close();
	cout << "您已成功回复!" << endl;
	}

bool show_My_Reply_message(string name) {
	ifstream infile("Reply_message.txt");
	if (!infile) {
		cout << "文件信息有误!" << endl;
		return false;
	}
	string line, cname,hname;
	int i = 0;
	while (getline(infile, line)) {
		size_t pos = 0,pos1=0;
		pos = line.find_first_of(' '); 
		cname = line.substr(pos + 8);
		hname = line.substr(pos+1);
		pos1 = hname.find_first_of(" ");
		pos = cname.find_first_of(' ');
		cname = cname.substr(0,pos);
		//cout << cname << endl;
		hname = hname.substr(pos1+10);
		//cout << hname<<endl;
		if (cname == name) {
			i++;
			cout << "我的第"<<i<<"条回复:" << endl;
			if ((sizeof(hname)) > 4) {
				string tname = hname.substr(0, 4);
				if (tname == "Hide") {
					size_t pos2 = 0; 
					pos2 = line.find_first_of("H");
					string str1 = line.substr(0,pos2);
					//cout << str1<<endl;
					string str2 = str1 + "匿名";
					cout << str2 << endl;
				}
				else cout << line << endl;
			}
			
		}

	}
	infile.close();
	if (i > 0) return false;
	if (i == 0) {
		cout << "您暂未发出回复!" << endl;
		return true;
	}
}

bool show_My_bottle(string name){
	ifstream inbottle("漂流瓶.txt");
	if (!inbottle) {
		cout << "文件信息有误!" << endl;
		return false;
	}
	string line, cname;
	int i = 0;
	while (getline(inbottle, line)) {
		size_t pos = 0;
		pos = line.find_first_of(' ');
		cname = line.substr(pos +8);
		//cout << cname<<endl;
		if (cname == name) {
			i++;
			cout << "您的第"<<i<<"个漂流瓶为:" << endl;
			cout << line << endl;
		}
	}
	inbottle.close();
	if (i > 0) return false;
	if (i == 0) {
		cout << "您暂未发出漂流瓶!" << endl;
		return true;
	}
}

bool show_Reply_to_me(string name) {
	ifstream readin("Reply_message.txt");
	if (!readin) {
		cout << "文件信息有误!" << endl;
		return false;
	}
	string line, cname;
	int i = 0;
	while (getline(readin, line)) {
		size_t pos = 0;
		pos = line.find_first_of(' ');
		cname = line.substr(pos + 8);
		pos = cname.find_first_of(' ');
		cname = cname.substr(pos+10);
		if ((sizeof(cname)) > 4) {
			string tname = cname.substr(0, 4);
			if (tname == "Hide") {
				cname = cname.substr(4);
			}
		}
		//cout << cname << endl;
		if (cname == name) {
			i++;
			cout << "我收到的第" << i << "条回复:" << endl;
			cout << line << endl;
		}

	}
	readin.close();
	if (i > 0) return false;
	if (i == 0) {
		cout << "您暂未收到回复!" << endl;
		return true;
	}

}



int main() {
	int x;
	User user;
	user.Loading();
	string id, name, password;
	cout << "欢迎进入漂流瓶系统!" << endl;
		cout << "请选择您想进行的操作:1.注册/2.登录/其他:退出" << endl;
		cin >> x;
		if (x == 1) {
			cout << "请输入您要注册的ID（可使用字母和阿拉伯数字）：" << endl;
			cin >> id;
			while ((user.IsRepetition(id)) == true)
			{
				cout << "对不起，您注册的ID已存在,请重新注册!" << endl;
				cout << "请输入您要注册的ID（可使用字母和阿拉伯数字）：" << endl;
				cin >> id;
			}
			cout << "请输入您喜欢的昵称：" << endl;
			cin >> name;
			cout << "请设置您的密码：" << endl;
			cin >> password;
			user.Register(id, name, password);
		}
		else if (x == 2) {
			int check;
			do {
				cout << "请输入您的ID:" << endl;
				cin >> id;
				cin.sync();
				cout << "请输入您的密码:" << endl;
				cin >> password;
				cin.sync();
				check = user.Login(id, password, name);
			} while (check == 1);
		}
		else  {
			cout << "感谢使用，再见！" << endl;
			exit(0);
		}
		
	
	char op;
	do {
		cout << "请选择您想进行的操作:t:扔漂流瓶/p:捡漂流瓶/3.查看已发出的漂流瓶/4.查看我发出的回复/5.查看我收到的回复/0:退出程序" << endl;
		cin >> op;
		if (op == 't') {
			throw_bottle(name);
		}
		else if (op == 'p') {
			char op2;
			string Rc;
			pick_bottle(Rc);
			cout << "你想回复这条漂流瓶吗？" << endl;
			cin >> op2;
			if (op2 == 'y' ||op== 'Y') {
				
				Reply_message(name,Rc);
			}
			else cout << "你没有选择回复!" << endl;
		}
		else if (op=='3') {
			show_My_bottle(name);
		}
		else if (op =='4') {
			show_My_Reply_message(name);
		}
		else if (op == '5') {
			show_Reply_to_me(name);
		}
	} while (op != '0');
	return 0;
}
